package com.cskaoyan.service.wx.impl;

import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.MarketOrderExample;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.vo.wx.wxuserindex.WXUserIndexTotalVo;
import com.cskaoyan.bean.vo.wx.wxuserindex.WXUserIndexVo;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.service.wx.WXUserIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Yan Dexuan
 * @since 2022/06/28 19:59
 */
@Service
public class WXUserIndexServiceImpl implements WXUserIndexService {

    @Autowired
    MarketOrderMapper orderMapper;

    /**
     * 微信小程序个人中心首页，显示待付款，待发货，待收货，待评价等订单数据信息。
     * @return com.cskaoyan.bean.vo.wxuserindex.WXUserIndexTotalVo
     * @author Yan Dexuan
     * @since 2022/06/28 22:06
     */
    @Override
    public WXUserIndexTotalVo useIndex(MarketUser marketUser) {

        //创建Example对象。
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        MarketOrderExample.Criteria criteria = marketOrderExample.createCriteria();

        //拼接条件。
        criteria.andUserIdEqualTo(marketUser.getId());

        //301
        int unrecv = 0;

        //单独查询。
        int unconmment = 0;

        //101
        int unpaid = 0;

        //201
        int unship = 0;

        List<MarketOrder> marketOrders = orderMapper.selectByExample(marketOrderExample);
        //生成状态码的数量。
        for (int i = 0; i < marketOrders.size(); i++) {
            if (marketOrders.get(i).getOrderStatus() == 101) {
                unpaid = unpaid + 1;
            } else if (marketOrders.get(i).getOrderStatus() == 201) {
                unship++;
            } else if (marketOrders.get(i).getOrderStatus() == 301) {
                unrecv++;
            }

            Short comments = marketOrders.get(i).getComments();
            unconmment+=comments;

            /*if (marketOrders.get(i).getComments() == 0) {
                unconmment++;
            }*/
        }

        //创建大Vo对象。
        WXUserIndexTotalVo wxUserIndexTotalVo = new WXUserIndexTotalVo();

        //调用小Vo对象中的封装方法。
        WXUserIndexVo wxUserIndexVo = WXUserIndexVo.setWXUserIndexVo(unrecv, unconmment, unpaid, unship);

        //封装。
        wxUserIndexTotalVo.setOrder(wxUserIndexVo);

        return wxUserIndexTotalVo;
    }

}
